package info.openlvb.openface.client.base.person;

import java.util.Iterator;
import java.util.List;

import info.openlvb.openface.client.base.Account;
import info.openlvb.openface.client.handler.AddGroupListener;
import info.openlvb.openface.client.handler.ClearGroupListener;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;

public class AddGroupPanel extends VerticalPanel {

	SuggestBox sb;
	MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
	Store store;
	GridPanel grid;
	int cour=-1;
	ColumnModel columnModel;
	TextField nom,description;
	RecordDef recordDef;
	
	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);
	
	public AddGroupPanel(){
		FormPanel formPanel = new FormPanel();
		formPanel.setFrame(true);
        formPanel.setTitle("Ajout d'un groupe");
        formPanel.setLabelWidth(75);
        
        nom = new TextField("Nom du groupe", "nom", 150);
        nom.setAllowBlank(false);
        formPanel.add(nom);
        description = new TextField("Description", "desc", 150);
        description.setAllowBlank(false);
        formPanel.add(description);
        
        recordDef = new RecordDef(  
				new FieldDef[]{  
						new StringFieldDef("pseudo"),
						new StringFieldDef("prenom"),
						new StringFieldDef("nom"),
						new StringFieldDef("mail"),
						new StringFieldDef("groupe"),
						new StringFieldDef("rep_access")
				}  
		);
		initOracle();
        sb = new SuggestBox(oracle);
        formPanel.add(sb);
        
        Button addmember = new Button("Ajouter comme membre");
        addmember.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				ldapService.searchAccountByUid(sb.getText(), new AsyncCallback<Account>() {
					public void onFailure(Throwable caught) {
						MessageBox.alert("probleme", "probleme de communication avec le backend");
					}
					public void onSuccess(Account result) {
						Record r = recordDef.createRecord(new Object[]{result.getLogin(),result.getPrenom(),result.getNom(),result.getMail(),result.getGroupe_principal(),result.getHome()});
						store.add(r);
						sb.setText("");
					}
				});
        	}
        });
        formPanel.add(addmember);
        Button addgroup = new Button("Ajouter ce groupe");
        addgroup.addListener(new AddGroupListener(this));
        formPanel.addButton(addgroup);
        Button cleargroup = new Button("Annuler");
        cleargroup.addListener(new ClearGroupListener(this));
        formPanel.addButton(cleargroup);
        ColumnConfig[] columns = new ColumnConfig[]{  
				//column ID is company which is later used in setAutoExpandColumn  
				new ColumnConfig("Pseudo", "pseudo", 120,true),
				new ColumnConfig("Nom", "nom", 75,true),
				new ColumnConfig("Prenom", "prenom", 100,true),
				new ColumnConfig("Mail", "mail", 200,true),
		};  
		columnModel = new ColumnModel(columns);
		grid = new GridPanel();
		store = new Store(recordDef);
		grid.setStore(store);  
		grid.setColumnModel(columnModel);  
		grid.setFrame(true);  
		grid.setStripeRows(true);  
		grid.setWidth(500);  
		grid.setHeight(150);  
		grid.setTitle("Liste des utilisateurs"); 
        
        add(formPanel);
        add(grid);
	}
	
	/**
	 * methode pour remplir l'oracle avec la liste des utilisateurs
	 * @author gbtux
	 * @param null
	 * @return void
	 */

	private void initOracle() {
		ldapService.searchUsersUid(new AsyncCallback<List<String>>() {
			public void onSuccess(List<String> result) {
				Iterator<?> i = result.iterator();
				while(i.hasNext()){
				      oracle.add((String) i.next());
				}
			}
			
			public void onFailure(Throwable caught) {
				MessageBox.alert("probleme", "probleme de communication avec le backend");
			}
		});
	}
	public TextField getNom() {
		return nom;
	}
	public TextField getDescription() {
		return description;
	}
	public SuggestBox getSuggestBox(){
		return sb;
	}
	public String[] getMembers(){
		Record[] members = store.getRecords();
		String[] result = new String [store.getRecords().length];
		for(int i=0;i<members.length;i++){
			result[i]=(String) members[i].getAsObject("pseudo");
		}
		return result;
	}
	public void setNullMembers(){
		store.removeAll();
		store.commitChanges();
		grid.removeAll();
	}
}
